package com.hrbxlh.virtualization.service.impl;

import com.hrbxlh.virtualization.dao.*;
import com.hrbxlh.virtualization.dto.ImageAllocationDTO;
import com.hrbxlh.virtualization.pojo.*;
import com.hrbxlh.virtualization.service.*;
import com.hrbxlh.virtualization.vo.ImageAllocationVO;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cheer
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private ImageAllocationService imageAllocationService;

    @Autowired
    private ClusterService clusterService;

    @Autowired
    private ContainerService containerService;

    @Autowired
    private ContainerMapper containerMapper;

    @Autowired
    private ImageAllocationMapper imageAllocationMapper;

    @Autowired
    private StatisticMapper statisticMapper;

    @Autowired
    private ClusterMapper clusterMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private ImageService imageService;

    private Logger logger = LoggerFactory.getLogger(CourseServiceImpl.class);

    @Override
    public List<ImageAllocationVO> queryImageAllocate(Integer chapterId) {
        List<ImageAllocationDTO> imageAllocationDTOList = imageAllocationService.queryImageAllocationByChapterId(chapterId);
        List<ImageAllocationVO> imageAllocationVOList = new ArrayList<>();
        for (ImageAllocationDTO imageAllocationDTO : imageAllocationDTOList) {
            ImageAllocationVO imageAllocationVO = imageAllocationDTO.convertToImageAllocationVO();
            if (imageAllocationVO.getCluster() && clusterService.isClusterBroken(imageAllocationVO.getClusterId())) {
                imageAllocationVO.setBroken(true);
            } else {
                imageAllocationVO.setBroken(false);
            }
            imageAllocationVOList.add(imageAllocationVO);
        }
        return imageAllocationVOList;
    }

    @Override
    public void deleteContainersByCourseId(List<Integer> courseIdList) {
        if (CollectionUtils.isEmpty(courseIdList)) {
            return;
        }

        ContainerExample containerExample = new ContainerExample();
        containerExample.or().andCourseIdIn(courseIdList);

        List<Container> containerList = containerMapper.selectByExample(containerExample);

        for (Container container : containerList) {
            try {
                containerService.deleteContainer(container);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    @Override
    public void deleteContainersByStuorteaId(List<String> stuorteaIdList) {
        if (CollectionUtils.isEmpty(stuorteaIdList)) {
            return;
        }

        ContainerExample containerExample = new ContainerExample();
        containerExample.or().andStuorteaIdIn(stuorteaIdList);
        List<Container> containerList = containerMapper.selectByExample(containerExample);

        for (Container container : containerList) {
            try {
                containerService.deleteContainer(container);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    @Override
    public void deleteImageAllocationByCourseId(List<Integer> courseIdList) {
        if (CollectionUtils.isEmpty(courseIdList)) {
            return;
        }

        ImageAllocationExample imageAllocationExample = new ImageAllocationExample();
        imageAllocationExample.or().andCourseIdIn(courseIdList);
        imageAllocationMapper.deleteByExample(imageAllocationExample);
    }

    @Override
    public void deleteImageAllocationByChapterId(List<Integer> chapterIdList) {
        if (CollectionUtils.isEmpty(chapterIdList)) {
            return;
        }

        ImageAllocationExample imageAllocationExample = new ImageAllocationExample();
        imageAllocationExample.or().andChapterIdIn(chapterIdList);
        imageAllocationMapper.deleteByExample(imageAllocationExample);

    }

    @Override
    public void deleteImageOrClusterByStuorteaId(List<String> stuorteaIdList) {
        if (CollectionUtils.isEmpty(stuorteaIdList)) {
            return;
        }

        ClusterExample clusterExample = new ClusterExample();
        clusterExample.or().andStuorteaIdIn(stuorteaIdList)
                .andSharedEqualTo(false);
        clusterMapper.deleteByExample(clusterExample);

        ImageExample imageExample = new ImageExample();
        imageExample.or().andStuorteaIdIn(stuorteaIdList)
                .andSharedEqualTo(false);
        List<Image> imageList = imageMapper.selectByExample(imageExample);

        for (Image image : imageList) {
            try {
                imageService.deleteImage(image.getId(), true);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }

    }

    @Override
    public void deleteStatisticByCourseId(List<Integer> courseIdList) {
        StatisticExample statisticExample = new StatisticExample();
        statisticExample.or().andCourseIdIn(courseIdList);
        statisticMapper.deleteByExample(statisticExample);
    }

    @Override
    public void deleteStatisticByStuorteaId(List<String> stuorteaIdList) {
        StatisticExample statisticExample = new StatisticExample();
        statisticExample.or().andStuorteaIdIn(stuorteaIdList);
        statisticMapper.deleteByExample(statisticExample);
    }
}
